<template>
  <view class="page-wrapper">
    <status-bar></status-bar>

    <!-- 主体显示区域 -->
    <view class="page-main">
      <image class="logo" src="/static/logo.png"></image>
      <view class="text-area">
        <text class="title">{{ title }}</text>
      </view>

      <uni-badge text="1"></uni-badge>
      <uni-badge text="7" type="primary" :inverted="true"></uni-badge>

      <button class="btn" @click="bindClick">sdfsdf</button>

      <view class="" v-for="(item, index) in info.history_data" :key="item.date">
        <text>{{ item.date }}--{{ item.netValue }}</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { reactive } from 'vue';

const title = ref('boxue is 666');

const info = reactive({
  history_data: [] // 基金的历史数据
});

const bindClick = () => {
  console.log('click');
  var xhr = new plus.net.XMLHttpRequest();

  const code = '007540';
  const url_1 = `https://ms.jr.jd.com/gw/generic/jj/h5/m/getFundHistoryNetValuePageInfo?reqData={"fundCode":"${code}","pageNum":1,"pageSize":300,"channel":"9"}`;

  xhr.open('GET', url_1);

  /*
{
  "resultCode": 0,
  "resultMsg": "操作成功",
  "resultData": {
    "code": "0000",
    "datas": {
      "titleMap": {
        "netValueRowName": "单位净值",
        "dateRowName": "日期",
        "totalNetValueRowName": "累计净值",
        "dailyRateRowRate": "日涨跌幅"
      },
      "netValueList": [
        {
          "date": "2025-02-19",
          "netValue": "1.1599",
          "dailyProfit": "0.15",
          "totalNetValue": "1.3240"
        },
*/
  xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
      if (xhr.status === 200) {
        try {
          const res = JSON.parse(xhr.responseText);
          if (res.resultCode === 0) {
            const resultData = res.resultData || {};
            const datas = resultData.datas || [];
            info.history_data = datas.netValueList || [];
          }
          console.log('Parsed JSON:', res);
        } catch (error) {
          console.error('Failed to parse JSON:', error.message || '转换json出错');
        }
      } else {
        console.error('Request failed. Status:', xhr.status);
      }
    }
  };

  xhr.send();
};
</script>

<style lang="scss" scoped>
.page-wrapper {
  position: fixed;
  width: 100%;
  height: 100vh;
  overflow: hidden;
  z-index: 5;
}
</style>
